// 路由相关方法
import Taro from "@tarojs/taro"
import { PagePath, pageMapper, tabbarPages } from "@/constants/pageMap"
import { querySerialize } from "@/utils/common"

/** 路由跳转
 * @mark 如果当前是首页，下一个要跳转的页面也是首页，则拦截跳转
 * @mark 如果当前是非首页，下一个要跳转的页面是首页，则reLaunch跳转
 */
export const navigation = function (
  path: PagePath,
  query?: Recordable | null,
  openType: "navigateTo" | "reLaunch" | "switchTab" | "redirectTo" = "navigateTo",
  isPluginPath = false,
  isGpPluginPath = false
) {
  const url = makePagePath(path, query, isPluginPath, isGpPluginPath)
  const currentPages = Taro.getCurrentPages()

  const currentPage = currentPages[currentPages.length - 1]?.route
  if (!currentPage) {
    // 如果路由找不到，说明可能是在插件页面调用的此组件
    return

    // 此方案无效
    // const xx = "plugin-private://wx13529b7297c4052c" + url
    // console.log("xx", xx)
    // Taro.switchTab({
    //   url: xx,
    //   success: res => {
    //     console.log()
    //     console.log("success", "plugin://yz-movies/movie_order")
    //   },
    //   fail: err => {
    //     console.log("fail", err)
    //   }
    // })
    // if (isIndexPage(path)) {
    //   console.log("currentPages11", url)
    //   Taro.reLaunch({
    //     url
    //   })
    // } else {
    //   console.log("currentPages22", url)
    //   Taro[openType]({
    //     url
    //   })
    // }
    // return
  }

  const toIndexPage = isIndexPage(path)
  if (isIndexPage(currentPage as any) && toIndexPage) {
    return
  }

  if (toIndexPage) {
    Taro.reLaunch({
      url
    })
    return
  }
  Taro[openType]({
    url
  })
}
/** 整合页面跳转路径 */
export const makePagePath = function (
  pathOrPluginPageName: PagePath,
  query?: Recordable | null,
  isPluginPath = false,
  isGpPluginPath = false
) {
  let search = ""
  if (query) {
    search = querySerialize(query, true)
  }

  // 高考插件
  let prefix = isPluginPath ? "plugin://myPlugin/" : "/"

  // 好学校插件
  if (isGpPluginPath) {
    prefix = "plugin://gpPlugin/"
  }

  const url = [prefix, pathOrPluginPageName, search].join("")
  return url
}
/** 常用导航 */
export const commonUrls = {
  /** 首页 */
  index: makePagePath(pageMapper.index),
  /** 院校查詢 */
  shoolQuery: makePagePath(pageMapper.shoolQuery),
  /** 专业大全 */
  professional: makePagePath(pageMapper.professional),
  /** 培训机构
   * @tabbarPage
   */
  institution: makePagePath(tabbarPages.gpPluginInstitution, null, false, true),
  /** 专业解读 */
  proExplain: makePagePath(pageMapper.proExplain),
  /** 招生简章 */
  introBadge: makePagePath(pageMapper.introBadge),
  /** 艺考专题-艺考百科 */
  column: makePagePath(pageMapper.column),
  /** 艺考专题-专题文章 */
  columnArticle: makePagePath(pageMapper.column, { sk: 1 }),
  /** 机构排名 */
  institutionRanking: makePagePath(pageMapper.institutionRanking),
  /** 艺考资讯 */
  news: makePagePath(pageMapper.news),
  /** 艺考动态 */
  examDynamic: makePagePath(pageMapper.examDynamic),
  /** 艺考政策 */
  examStrategy: makePagePath(pageMapper.examStrategy),
  /** 艺考问答 */
  examQA: makePagePath(pageMapper.examQA),
  /** 报考指南(预约试听页) */
  examGuide: makePagePath(pageMapper.examGuide),
  /** 培训指南 */
  trainGuide: makePagePath(pageMapper.trainGuide),
  /** 算分神器 */
  calculator: makePagePath(pageMapper.calculator)
}
/** 是否是首页 */
export const isIndexPage = (path: PagePath): boolean => {
  return path === pageMapper.index
}
/** 跳转到小程序插件
 */
export const navigationPlugin = function (pageName: string, query?: Recordable | null) {
  const url = makePagePath(pageName, query, true)
  Taro.navigateTo({
    url
  })
}
